var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
var page = 1;
var wxAuthFail = true; 

var inviteuid = '';

Page({  
  data: {  
    uid: 0,
    tc: 0,
    token: '',
    userinfo:{},


    colorSet: '',

    hasShare: false,

    bottom_nav_data: [],
    isIphone_x: false,
    nav_bgColor: '#ffffff',

    userinfo: {},
    menu:false, //是否需要绑定

    studentInfo:[],  //学习获取自己信息
     coachInfo:[],  //教练信息
     recordsInfo:[],  //缴费信息
  },  
   
  onLoad: function (data) {  


    var that = this;
    var tc = app.get_tc();//简体繁体转换
    that.setData({
      tc: tc
    })
    var titlename = tc == 1 ? '东和菜单' : '东和菜单';
    wx.setNavigationBarTitle({
      title: titlename,
    }); 
 
    //初始化底部导航
    var b_nav_data = app.getBottomNavData('east_menu/east_menu', function (data) {
      that.setData({
        bottom_nav_data: data,
        nav_bgColor: data[0].bgcolor
      });
    });
    //获取颜色配置
    app.getColorSetData(function (color) {
      that.setData({
        colorSet: color,
      })
      // that.resetNavColor();
    });

    wx.getSystemInfo({
      success: function (res) {
        var isIphone_x = (res.model.indexOf('iPhone X') >= 0);
        that.setData({
          isIphone_x: isIphone_x,
        });
      }
    });

     //当前登录用户信息
    app.LoginUserInfo(function (res) {
      var authFail = res == -1;//用户拒绝授权
      if (!authFail) {
        //更新数据
        that.setData({
          uid: res.uid,
          token: res.token,
          openid: res.wxsmall_openid
        });


      }

    });


    that.studentGetinfo(data.studentId);
    
 
  },  


  onShow: function () {

  },




   //学员页面--获取自己的信息
  studentGetinfo:function(studentId){
    var that = this;
    util.api({
        data: {
          "publickey": util.publiccode,
          "ac": "studentGetinfo",
          "uid": that.data.uid,
          "studentId":studentId,
        },
        
        success: function (responseData) {
          console.log("学生信息==", responseData.data);

          that.setData({
            studentInfo:responseData.data
          })

          that.coachGetinfo(responseData.data.coachId);
          that.getRecords(responseData.data.id);
        } 
      });
  },


  //获取教练信息
  coachGetinfo:function(coachId){
    var that = this;
    util.api({
        data: {
          "publickey": util.publiccode,
          "ac": "coachGetinfo",
          "uid": that.data.uid,
          "coachId":coachId,
        },
        
        success: function (responseData) {
          console.log("教练信息==", responseData.data);
          that.setData({
            coachInfo:responseData.data
          })
        } 
      });
  },

  //获取学员缴费信息
  getRecords:function(studentId){
    var that = this;
    util.api({
        data: {
          "publickey": util.publiccode,
          "ac": "getRecords",
          "uid": that.data.uid,
          "studentId":studentId,
        },
        
        success: function (responseData) {
          console.log("缴费信息==", responseData.data);
          var result = responseData.data;
          if(result.res != false){
            for(var i = 0; i<result.length;i++){
              var subTime = result[i].subTime;
              if(subTime != ''){
                result[i].subTime = subTime.replace(/-/g, '/');
              }

              var payTime = result[i].payTime;
              if(payTime != ''){
                result[i].payTime = payTime.replace(/-/g, '/');
              }
            }
            
            that.setData({
              recordsInfo:result
            })
          }
          
        } 
      });
  },





  //跳转页面
  goAtherPage: function (e) {
    var url = e.currentTarget.dataset.link;
    if (url) {
      wx.navigateTo({ url: url });
    }
  },

  //保存formId
  submitFormId: function (e) {
    app.saveFormId(e.detail.formId);

    var path = e.detail.value.nav_path;
    if (path && path.indexOf('navto:') == 0) {
      var navPath = path.slice(6);
      wx.navigateTo({
        url: navPath,
      })
    } else {
      wx.redirectTo({ url: path });
    }
  },
})
